import Logger from './Logger';
import shimeta from '@ohos.shimeta';
import storageStatistics from '@ohos.file.storageStatistics';
import { BusinessError } from '@ohos.base';
import inputMonitor from '@ohos.multimodalInput.inputMonitor';

const TAG = `DevUtils`

export default class DevUtils {
  public static getTotalStorage() {
    let ret: string = shimeta.info_getTotalStorage()
    return DevUtils.bytesToGB(Number(ret)).toString() + `GB`;
  }

  public static bytesToGB(byte: number) {
    // 将字节转换为千字节
    let MB = byte / 1000;
    let GB = MB / 1000;
    let parts = GB.toString().split('.');
    //若总储存后存在小数，则向前加一位
    if (parts.length > 1) {
      GB = parseFloat(parts[0]);
      return GB + 1;
    }
    // Logger.d(TAG, "return  GB :" + GB);
    return GB;
  }

  public static async getAvailableStorage() {
    let number = await storageStatistics.getFreeSize()

    let availableStorage = (number / 1024 / 1024 / 1024).toFixed(2).toString();
    Logger.d(TAG, "getAvailableStorage successfully:" + JSON.stringify(availableStorage));
    return availableStorage + `GB`
  }

  public static getTotalRam() {
    let result: string = shimeta.info_getTotalMemory();
    return DevUtils.bytesToGB(Number(result)).toString() + `GB`;
  }

  public static getAvailableRam() {
    let result: string = shimeta.info_getAvailMemory()
    let availableRam = (Number(result) / 1024 / 1024).toFixed(2);
    return availableRam + `GB`
  }

  public static enableBacklight() {
    let ret = shimeta.disp_setLcdBackLightEnable(0, false)

    Logger.i(TAG, 'enableBacklight(false) ret :'+ret);

    setTimeout(function () {
      shimeta.disp_setLcdBackLightEnable(0, true)
      Logger.i(TAG, 'enableBacklight(true) ret :'+ret);

    }, 10*1000);
  }

  public static doSomeThing(){

    
  }

  public static install() {
    try {
      // @ts-ignore
      inputMonitor.on('mouse', (mouseEvent: MouseEvent) => {
        Logger.d(TAG, `Monitor on success ${JSON.stringify(mouseEvent)}`);
        return false;
      });
    } catch (error) {
      Logger.d(TAG, `Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
    }
  }

  public static checkMacRange(mac): boolean {

    const macRegex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;

    return macRegex.test(mac);
  }
}

